import { getTopicalList, praise } from "../../api/case";
import { getAssistanceList } from "../../api/guideai";
import { replySeminar } from "../../api/seminar";
import { debounce } from "../../utils/debounce";

Page({
  /**
   * 页面的初始数据
   */
  data: <any>{
    active: 1,
    showJoinWebinarDialog: false,
    detail: {},
    interactive: [],
    assistance: [],
    reply: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const _self = this;
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      if(data && data.id){
        _self.setData({
          detail: data
        })
      }
    })
  },

  joinHandle(){
    this.setData({
      showJoinWebinarDialog: true
    })
  },

  closeJoinWebinarDialog(){
    this.setData({
      showJoinWebinarDialog: false
    })
  },

  caseHandle(data: any){
    const { index } = data.currentTarget.dataset
    this.setData({
      active: index
    })
    const type: any = {
      '2': {
        info: this.data.interactive,
        api: this.getInteractive,
      },
      '3': {
        info: this.data.assistance,
        api: this.getAssistance,
      }
    };
    setTimeout(() => {
      const caseHandle = type[index];
      if((index === '2' || index === '3') && caseHandle.info && caseHandle.info.length === 0){
        caseHandle.api()
      }
    }, 0)

  },

  getInteractive(){
    getTopicalList({
      pageNo: 1,
      pageSize: 100,
      caseId: this.data.detail.id
    }).then((res: any) => {
      if(res.rows){
        const rows: any = res.rows.map((row: any) => {
          if(row.replyList){
            row.replyList.forEach(element => {
              if(element.userVo && !element.userVo.avatar){
                element.userVo.avatar = '/static/img/home/avatar1.png'
              }
            });
          }
          return {
            ...row,
            isReplyInputVisible: false
          }
        })
        this.setData({
          interactive: rows
        })
      }

    })
  },

  getAssistance(){
    getAssistanceList({
      pageNo: 1,
      pageSize: 100,
      caseId: this.data.detail.id
    }).then(res => {
      if(res.rows && res.rows.length > 0){
        this.setData({
          assistance: res.rows
        })
      }
    })
  },
  /**
   * 输入
   */
  onEditorInput(e: any){
    this.setData({
      reply: e.detail.text
    });
  },

  likeHandle: debounce(async (e: any, context: any) => {
    const id = e.currentTarget.dataset.id;
    const index = e.currentTarget.dataset.index;
    const res = await praise(id)
    const interactive = [...context.data.interactive]
    interactive[index].isPraise = !interactive[index].isPraise
    interactive[index].praiseNum = res.data.praiseNum
    context.setData({
      interactive 
    })
  }),
  
  /**
   * 回复
   */
  replyHandle(e: any){
    const index = e.currentTarget.dataset.index;
    const items = this.data.interactive;
    items[index].isReplyInputVisible = true;
    this.setData({
      interactive: items
    });
  },
  reply(e){
    const id = e.currentTarget.dataset.id;
    if(!id) return;
    if(!this.data.reply){
      return
    }
    replySeminar({
      topicalId: id,
      content: this.data.reply
    }).then(res => {
      wx.showToast({
        title: '回复成功',
        icon: 'success',
        duration: 2000
      })
      this.getInteractive()
    })
  },

  onErrorInter(e: any){
    let { index } = e.currentTarget.dataset;
    let interactive = this.data.interactive;
    interactive[index].userVo.avatar = '/static/img/home/avatar.png'
    this.setData({
      interactive
    })
  },

  onError(e: any) {
    let { index, i } = e.currentTarget.dataset;
    let interactive = this.data.interactive;
    interactive[index].replyList[i].userVo.avatar = '/static/img/home/avatar.png'
    this.setData({
      interactive
    })
  },

  backToCase(){
    wx.navigateBack({
      delta: 1
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    try {
      this.data.requestTask.abort()
    } catch (error) {
      console.log('还没发请求就退出了，正常不会有影响')
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      "title": "",
      "path": `pages/home/home`,
      "imageUrl": "/static/img/home.jpg"
    }
  }
})